package com.blb.demo3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Scanner;
import java.util.Stack;

public class UDPServer {

    public static final int PORT=8888;


    public void start(){
        System.out.println("启动UDP服务器");


        try {
            DatagramSocket datagramSocket = new DatagramSocket(PORT);
            while (true){
                byte[] data=new byte[1024];
                DatagramPacket datagramPacket = new DatagramPacket(data, 0, data.length);
                datagramSocket.receive(datagramPacket);
                String s = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
                System.out.println("服务器收到:"+s);

            }


        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


    public static void main(String[] args) {
        new UDPServer().start();
    }

}
